<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class FileController
{
    public function upload(Request $request)
    {
        // 获取上传的文件
        $file = $request->file('file');

        // 检查文件是否有效
        if (!$file || !$file->isValid()) {
            return response()->json(['error' => 'Invalid file'], 400);
        }

        // 获取文件后缀
        $extension = $file->getClientOriginalExtension();

        // 生成独一无二的文件名
        $uniqueFileName = Str::uuid() . '.' . $extension;

        // 将文件存储到阿里云 OSS
        $filePath = $file->storeAs('', $uniqueFileName, 'oss');

        // 返回存储路径或其他响应信息
        return response()->json(['url' => Storage::disk('oss')->url($filePath)]);
    }
}
